package struts.action.departement;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import metier.Departement;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

import struts.actionForm.departement.BeanConnexionDepartement;

import dao.DAODepartement;

public class ActionConnexionDepartement extends Action 
{
	private DAODepartement DAODep =null;

	public DAODepartement getDAODep() 
	{
		return DAODep;
	}

	public void setDAODep(DAODepartement DAODep) 
	{
		this.DAODep = DAODep;
	}
	
	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)	throws Exception
	{
		try 
		{
			BeanConnexionDepartement beanDep= (BeanConnexionDepartement) form;
			Departement dep = DAODep.get(beanDep.getLogin(), beanDep.getMdp());
			
			if(dep==null)
			{
				throw new Exception();				
			}
			
			request.getSession().setAttribute("DEPARTEMENT", dep);
			
		} catch (Exception e) 
		{
			ActionErrors errors = new ActionErrors();
			errors.add("Login ou/et mot de passe incorrect(s) !", new ActionMessage("departement.erreur.login_mdp"));
			saveErrors(request, errors);
			return mapping.findForward("ERREUR");
		}
		
		return mapping.findForward("ActionNouvellesOffresDeStage");
	}

}
